﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Xml;
using System.Data.SqlClient;

namespace SgAutoArchiveSp.Dialog
{
    public partial class Dialog_DBConfig : DevExpress.XtraEditors.XtraForm
    {

        private readonly string _sXml = Application.StartupPath + "\\SgAutoArchiveBrr.exe.config";
        private string _sConfig1 = "metadata=res://*/SgAutoArchiveDal.csdl|res://*/SgAutoArchiveDal.ssdl|res://*/SgAutoArchiveDal.msl;provider=System.Data.SqlClient;provider connection string=\""; //"metadata=res://*/Linq.SysDB.csdl|res://*/Linq.SysDB.ssdl|res://*/Linq.SysDB.msl;provider=System.Data.SqlClient;provider connection string=\"";
        private string _sCOnfig2 = "MultipleActiveResultSets=True;App=EntityFramework\""; //"MultipleActiveResultSets=True\"";

        private string _sValue = "";
        public Dialog_DBConfig()
        {
            InitializeComponent();

            LoadDBConfig();
        }

        private void LoadDBConfig()
        {
            //metadata=res://*/QRScanSystemEntity.csdl|res://*/QRScanSystemEntity.ssdl|res://*/QRScanSystemEntity.msl;provider=System.Data.SqlClient;provider connection string=&quot;
            //data source=SYONPC;initial catalog=QRScanSystemNew;persist security info=True;user id=sa;password=syonlin;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient
            //data source=SYONPC;initial catalog=QRScanSystemNew;persist security info=True;user id=sa;password=syonlin;
            XmlDocument doc = new XmlDocument();
            doc.Load(_sXml);
            var node = doc.GetElementsByTagName("connectionStrings")[0];
            string value = node.ChildNodes[0].Attributes["connectionString"].Value;
            string server = value.Substring(value.IndexOf("data source=") + 1);
            server = server.Substring(11, server.IndexOf(";") - 11);
            txt_Server.Text = server;
            string uid = value.Substring(value.IndexOf("user id=") + 1);
            uid = uid.Substring(7, uid.IndexOf(";") - 7);
            txt_User.Text = uid;
            string pwd = value.Substring(value.IndexOf("password=") + 1);
            pwd = pwd.Substring(8, pwd.IndexOf(";") - 8);
            txt_Password.Text = pwd;
            _sValue = value;
        }

        private void btn_Test_Click(object sender, EventArgs e)
        {
            string server = "server=" + txt_Server.Text + ";uid=" + txt_User.Text + ";pwd=" + txt_Password.Text;
            SqlConnection conn = new SqlConnection(server);
            try
            {
                conn.Open();
                MessageBox.Show("测试成功!");
            }
            catch (Exception ex) { MessageBox.Show("测试失败! " + ex.Message); }
            finally
            {
                conn.Close();
            }
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(_sXml);
                var node = doc.GetElementsByTagName("connectionStrings")[0];
                string value = _sConfig1 + "data source=" + txt_Server.Text + ";initial catalog=SgAutoArchiveBrr;persist security info=True;" +
                        "user id=" + txt_User.Text + ";password=" + txt_Password.Text + ";" +
                        _sCOnfig2;
                node.ChildNodes[0].Attributes["connectionString"].Value = value;
                doc.Save(_sXml);
                //StreamReader sr = new StreamReader(
                MessageBox.Show("保存成功! 请重新启动软件，不然配置无法生效!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存设置发生错误 具体错误: " + ex.Message);
            }
        }
    }
}